Sometimes we need to raise new subtasks when other subtasks are finished.

In this example we explain how we can do this in Jira using the famous plugin Adaptavist Scriptruner.

The trigger of the action is the “transition”: when someone moves a subtask from one status to another. In this case, from “IN PROGRESS” to “CLOSED”. In the example we assume that all IssueTypes are using the same Workflow.

Captura de pantalla 2018-05-04 a las 17.39.48

Then we can add a PostFunction, in this case is a “Custom Script Post Function” in Scriptrunner, but probably we can use Groovy Amigo also to do that.

Try to add this code and replace some parts to make it work in your side!


import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager;
import com.opensymphony.workflow.WorkflowContext;
import org.apache.log4j.Category;
import com.atlassian.jira.config.SubTaskManager;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.util.UserManager

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
UserManager userManager = ComponentAccessor.getUserManager();
def user2 = userManager.getUserByName("admin")

MutableIssue parent = (MutableIssue) issue.getParentObject();
if ( issue.issueType.name == "Issuetype name trigger of the action") {
    //We create the new Subtask
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary("XXX - "+parent.summary)
    newSubTask.setParentObject(parent)
    newSubTask.setProjectObject(parent.getProjectObject())
    newSubTask.setAssignee(userManager.getUserByName("xxx.xxx"))
    newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
        it.getName() == "IssueType target"
    }.id)
    Map newIssueParams = ["issue" : newSubTask] as Map
    issueManager.createIssueObject(currentUser, newIssueParams)
    subTaskManager.createSubTaskIssueLink(parent, newSubTask, user2)

}

Publish the Workflow and try it!
That’s all!

IMPORTANT NOTE: WordPress always remove the tags of codes listed in the blog, be careful! See below picture of the line of code for “Map” instance (you will see the HTML tags between “String,Object”):

Captura de pantalla 2018-05-04 a las 18.02.53

By MrAddon

 

Posted by:.

Leave a comment